﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Configuration;
using Nano.Core;
using Nano.Core.Config;

namespace Nano.App_Config
{
    public partial class SiteLayout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                foreach (var item in Directory.GetDirectories(Server.MapPath("~/App_Layouts/")))
                {
                    ListBox1.Items.Add(item.Substring(item.LastIndexOf(Path.DirectorySeparatorChar) + 1));
                }
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            Literal1.Text = ConfigEngine.Instance.Layout;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex > -1)
            {
                ConfigEngine.Instance.Layout = ListBox1.SelectedItem.Text;
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex > -1)
            {
                string fileContent = File.ReadAllText(Server.MapPath("~/App_Layouts/" + ListBox1.SelectedItem.Text + "/Site.master"));

                fileContent = fileContent.Substring(fileContent.IndexOf("<body>") + 6);

                fileContent = fileContent.Remove(fileContent.LastIndexOf("</body>"));

                div1.InnerHtml = fileContent;
            }
        }
    }
}